"""文本处理服务"""
import os
import yaml
from pathlib import Path
import ollama

class TextService:
    def __init__(self):
        """初始化文本服务"""
        # 加载配置
        config_path = Path(__file__).parent.parent.parent / "config" / "ollama.yaml"
        with open(config_path) as f:
            self.config = yaml.safe_load(f)
        
        # 设置Ollama环境变量
        os.environ["OLLAMA_HOST"] = f"{self.config['host']}:{self.config['port']}"
        
        # 模型配置
        self.model = self.config["model"]["name"]
        self.temperature = self.config["model"]["temperature"]
        self.max_tokens = self.config["model"]["max_tokens"]
    
    async def process_text(self, text: str) -> str:
        """处理文本"""
        try:
            # 调用Ollama模型
            response = ollama.generate(
                model=self.model,
                prompt=text,
                temperature=self.temperature,
                max_tokens=self.max_tokens
            )
            return response['response']
            
        except Exception as e:
            print(f"文本处理错误: {str(e)}")
            return text 